﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace KInterfaces
{
    /// <summary>
    /// 运行任务的执行状态
    /// </summary>
    public class KJob
    {
        /// <summary>
        /// 业务名称
        /// </summary>
        public string Name { get; private set; }
        /// <summary>
        /// 模块程序名称
        /// </summary>
        public string ModuleAppName { get; set; }    
        public string GUID { get; private set; }
        public DateTime StartTime { get; private set; }
        public DateTime StopTime { get; private set; } 
        public JobStatus Status { get; private set; }
        public string Message { get; private set; }
        public long ElapsedTime { get; private set; }

        public int ErrorCode { get; private set; }

        /// <summary>
        /// 输入参数
        /// </summary>
        public string InParams { get; private set; }

        /// <summary>
        /// 输出参数
        /// </summary>
        public string OutParams { get; private set; }

        private Stopwatch sw = new Stopwatch();

        public event JobStateChangedEventHandler StateChanged;

        protected void OnStateChanged()
        {
            if(StateChanged != null)
            {
                StateChanged(this, new JobStateChangedEventArgs(this));
            }
        }

        public KJob(string module, string name, string uid, string inParams = "")
        {
            ModuleAppName = module;
            Name = name;
            GUID = uid;
            Status = JobStatus.Ready;
            StartTime = DateTime.MinValue;
            StopTime = DateTime.MinValue;
            ElapsedTime = 0L;
            Message = "";
            InParams = inParams;
            OutParams = "";
        }

        public void OnReady()
        {
            StartTime = DateTime.Now;
            Message = $"业务{Name}已准备就绪.";
            Status = JobStatus.Ready;
            ErrorCode = 0;
            OnStateChanged();
        }

        public void Start()
        {
            StartTime = DateTime.Now;
            Message = $"业务{Name}已启动.";
            Status = JobStatus.Started;
            ErrorCode = 1;
            sw.Start();
            OnStateChanged();
        }

        public void OnFinished(string outParams = "")
        {
            sw.Stop();
            Status = JobStatus.Completed;
            StopTime = DateTime.Now;
            ElapsedTime = sw.ElapsedMilliseconds;
            ErrorCode = 10;
            Message = $"业务{Name}已完成";
            OutParams = outParams;
            OnStateChanged();
        }

        public void Abort(string reason, int code = -13)
        {
            sw.Stop();
            StopTime = DateTime.Now;
            Status = JobStatus.Aborted;
            ElapsedTime = sw.ElapsedMilliseconds;
            Message = $"业务{Name}已中止: {reason}";
            ErrorCode = code;
            OnStateChanged();
        }

        public void Cancel(string reason)
        {
            sw.Stop();
            StopTime = DateTime.Now;
            Status = JobStatus.Cancelled;
            ElapsedTime = sw.ElapsedMilliseconds;
            Message = $"业务{Name}已取消: {reason}";
            ErrorCode = -10;
            OnStateChanged();
        }

        public void OnError(string error, int code = -15)
        {
            sw.Stop();
            Status = JobStatus.Error;
            ElapsedTime = sw.ElapsedMilliseconds;
            StopTime = DateTime.Now;
            Message = $"业务{Name}发生了错误:{error}";
            ErrorCode = code;
            OnStateChanged();
        }
    }
}
